package com.qfedu.meidijiaju.service.impl;

import com.qfedu.meidijiaju.common.ResponseData;
import com.qfedu.meidijiaju.entity.PinglunYxy;
import com.qfedu.meidijiaju.mapper.FaxiantieziYxyMapper;
import com.qfedu.meidijiaju.mapper.PinglunYxyMapper;
import com.qfedu.meidijiaju.service.PinglunYxyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author meidimeiju
 * @since 2022-10-04
 */
@Service
public class PinglunYxyServiceImpl extends ServiceImpl<PinglunYxyMapper, PinglunYxy> implements PinglunYxyService {

    @Autowired
    private PinglunYxyMapper pinglunYxyMapper;

    @Autowired
    private FaxiantieziYxyMapper faxiantieziYxyMapper;

    @Override
    public ResponseData<List<PinglunYxy>> findPinglun(int tieziId) {

        ResponseData<List<PinglunYxy>> responseData = new ResponseData<>();

        List<PinglunYxy> pinglunYxyList = pinglunYxyMapper.findPinglun(tieziId);

        int result = faxiantieziYxyMapper.updateFaXianTieZiByPingLunNum(pinglunYxyList.size(),tieziId);

        List<PinglunYxy> pinglunYxies = pinglunYxyMapper.findPinglun(tieziId);

        if (pinglunYxies != null && pinglunYxies.size() > 0 ){

            responseData.setCode(200);

            responseData.setData(pinglunYxies);

        } else {

            responseData.setCode(400);

            responseData.setMsg("无评论！");

        }
        return responseData;
    }

    @Override
    public ResponseData<String> addPingLun(PinglunYxy pinglunYxy) {

        ResponseData<String> responseData = new ResponseData<>();

        if (pinglunYxy != null && !StringUtils.isEmpty(pinglunYxy.getPingluncontent())){

            int result = pinglunYxyMapper.addPingLun(pinglunYxy.setPinglundate(new Date()));

            if (result == 1){

                responseData.setCode(200);

                responseData.setMsg("评论成功！");
            } else {

                responseData.setCode(400);

                responseData.setMsg("评论失败！");
            }
        } else {

            responseData.setCode(400);

            responseData.setMsg("请输入评论内容！");
        }
        return responseData;
    }
}
